package com.bootx.controller.api;

import com.bootx.common.Pageable;
import com.bootx.common.Result;
import com.bootx.entity.BaseEntity;
import com.bootx.entity.Lottery;
import com.bootx.entity.LotteryRecord;
import com.bootx.service.LotteryRecordService;
import com.bootx.service.LotteryService;
import com.bootx.service.RedisService;
import com.bootx.util.DateUtils;
import com.bootx.util.JsonUtils;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.type.TypeReference;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author black
 */
@RestController
@RequestMapping("/api/lottery")
public class LotteryController {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private LotteryService lotteryService;
    @Resource
    private LotteryRecordService lotteryRecordService;
    @Resource
    private RedisService redisService;

    @PostMapping(value = "/list")
    @JsonView(BaseEntity.PageView.class)
    public Result list(){
        return Result.success(lotteryService.findAll());
    }

    @PostMapping(value = "/edit")
    public Result edit(String code){
        return Result.success(jdbcTemplate.queryForMap("select id,code,name,baseBetMoney,baseTimes,continuousLostTimes,maxBetMoney,odds,predictionCount,walletIndex from lottery where code=? limit 1",code));
    }

    @PostMapping(value = "/result")
    @JsonView(BaseEntity.IdView.class)
    public Result result(String date, String code, Pageable pageable){
        if(StringUtils.isBlank(date)){
            date = DateUtils.formatDateToString(new Date(),"yyyy-MM-dd");
        }
        if(StringUtils.isBlank(code)){
            code = "10036";
        }
        return Result.success(lotteryRecordService.findPage(pageable,date,code));
    }

    @PostMapping(value = "/update1")
    public Result update1(Boolean win,String sn){
        int update = jdbcTemplate.update("update lotteryrecord set win=? where sn=?", win, sn);
        return Result.success();
    }

    @GetMapping(value = "/config")
    public Map<String,Object> config(){
        Map<String,Object> data = new HashMap<>();
        data.put("open",0);
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select startTime,endTime from lottery where code=10036");
        if(!maps.isEmpty()){
            String dateStr = DateUtils.formatDateToString(new Date(),"yyyy-MM-dd");
            Date start = DateUtils.formatStringToDate(dateStr+" "+maps.getFirst().get("startTime"),"yyyy-MM-dd HH:mm:ss");
            Date end = DateUtils.formatStringToDate(dateStr+" "+maps.getFirst().get("endTime"),"yyyy-MM-dd HH:mm:ss");
            data.put("open",new Date().compareTo(start) >= 0 && new Date().compareTo(end) <= 0?1:0);
        }
        data.put("predictNum",redisService.get("predictNum").replace(",",""));
        data.put("sendMoney",redisService.get("sendMoney"));
        data.put("isSend",redisService.get("isSend"));
        return data;
    }
}
